import { filePathDefault, metaNameDefault, ViteAppVersionConfig } from "./appVersionConfig"

export async function appVersionCheck(config?: ViteAppVersionConfig, logDetail?: boolean) {
    const metaName = config?.metaName || metaNameDefault
    const filePath = config?.filePath || filePathDefault
    const requestPath = '/'+filePath
    const timestampInFile = await (await fetch(requestPath, { cache: 'no-store' })).text()
    const timestampInHtml = (document.querySelector(`meta[name=${metaName}]`) as HTMLMetaElement)?.content
    const res = timestampInFile === timestampInHtml
    if (logDetail) {
        console.log(`[vav] responded: ${timestampInFile}`)
        console.log(`[vav] localRead: ${timestampInHtml}`)
        if (res)
            console.log(`version checking okay`)
        else
            console.warn(`version checking not okay`)
    }
    return res
}